<?php
// 添加淘宝入口
require_once ("log.php");
error_reporting(E_ERROR);
require_once ("cloud_api.php");

class cloud_shop {
	// 进入主界面
	function my_shop() { 
		global $v_file;
	
		$cs_title = "";
		$good_list = array ();

		$cat_list = AllQuery("SELECT * FROM parent_category");
		for ($i = 0; $i < count($cat_list); ++$i) {
			$row = $cat_list[$i];
			$good_list[$row['weigh']] = array (
				'goodInfo' 		=> $row['name'],
				'goodSearch' 	=> $row['id'],
				'goodImg' 		=> $row['image'],
			);
		}
		ksort($good_list);
		foreach ($good_list as $key => $value) {
			$cs_title .= "<li>";
			$cs_title .= "<a class = 'nav-a' name = 'title' data_img = '{$value['goodImg']}' data_search = {$value['goodSearch']} data_name = {$value['goodInfo']} data_id = {$key} onclick = refresh_interface({$key}) href = 'javascript:0;'>{$value['goodInfo']}</a>";
			$cs_title .= "</li>";
		}

		require($v_file);
	}

	// 获取淘宝列表
	function get_good_new() {
		global $mysql_conn;
		$page = $_GET['page'];
		// 添加搜索(除了全部和其他单独处理,其他根据各自字段进行处理)
		$search = $_GET['search'];
		$name = $_GET['name'];
		if (!empty($search)) {
			if ($name == "全部") {
				$where = "";
			} else {
				$where = "WHERE parent_category_ids LIKE '%,{$search},%'";
			}
		}
	
		$rt = array();
		$item_list = AllQuery("SELECT * FROM item_ordinary {$where} LIMIT {$page}, 5");
		LOGINFO(count($item_list), '=== count($item_list)');
		for ($i = 0; $i < count($item_list); ++$i) {
			$row = $item_list[$i];
			// 进行特殊处理
			if (empty($row['coupon_amount'])) {
				$row['coupon_amount'] = '0';
			}

			if (empty($row['coupon_start_fee'])) {
				$row['coupon_start_fee'] = '0';
			}

			$amo_list = explode(',', $row['coupon_amount']);
			$fee_list = explode(',', $row['coupon_start_fee']);
			$amo_data = $amo_list[0];
			$fee_data = $fee_list[0];
			if (count($amo_list) > 1) {
				for ($i = 0; $i < count($amo_list); ++$i) {
					$e = $shop_fee_price[$i];
					if ($amo_data < $amo_list[$i] && $row['price'] > $fee_data[$i]) {
						$amo_data = $amo_list[$i];
						$fee_data = $fee_list[$i];
					}
				}
			}

			$em = array();
			$em['item_id'] 					= $row['item_id']; 			// 商品唯一标识码
			$em['nick'] 					= $row['nick'];				// 商铺名称
			$em['name'] 					= $row['name'];				// 商品名称
			$em['image']					= $row['image'];			// 图案
			$em['price']					= $row['price'];			// 价格
			$em['coupon_amount']			= $amo_data;				// 优惠券金额
			$em['coupon_start_fee']			= $fee_data;				// 优惠券启用金额
			$em['channel_id']				= $row['channel_id'];		// 获取渠道代码
			array_push($rt, $em);
		}
		echo json_encode($rt);
	}

	// 发送http请求()
	function send_group_data() {
		global $mysql_conn;
		// 根据当前数据获取http地址
		$bot_id = $_COOKIE['bot_id'];
		$item_id = $_GET['item_id'];
		$channel_id = $_GET['channel_id'];
		$good_img = $_GET['good_img'];
		$_serial = $_GET['_serial'];

		if (empty($bot_id)) {
			$rt = array (
				'state' 	=> -6,
				'msg'  		=> '已经超过该操作时间!',
			);
			echo json_encode($rt); // 操作过时
			exit;
		}
		$addr = simQuery("SELECT ser.host AS host, ser.port AS post FROM wechat_group AS wx, robot AS rot, `server` AS ser WHERE wx.id = {$_serial} AND wx.robot_id = rot.id AND rot.server_id = ser.id");
		if (empty($addr)) {
			$rt = array (
				'state' 	=> -1,
				'msg'		=> '没有该服务器地址!',
			);
			echo json_encode($rt);
			exit();
		}

		$host 				= $addr['host'];		// 设置http请求地址
		$port				= $addr['port'];		// 设置http请求端口
		$data['shop_id']	= $item_id;				// 设置商品id
		$data['wx_id'] 		= $_serial;				// 设置微信id
		$data['good_img'] 	= $good_img;			// 设置发送的开头图片
		$data['channel_id'] = $channel_id;			// 设置发送商品的类型
		$data['shop_code']	= '1';					// 设置发送的商品库
		$data['fun'] 		= 'send_shop_group';	// 调用的商品函数
		$data['sign'] 		= generateSign($data);
	
		echo sendZSHttp($host, $port, '/api/api_accept_post.php', $data);
		exit();
	}

	// 展示群信息
	function show_sg_data() {
		global $v_file, $mysql_conn;
		$bot_id = $_COOKIE['bot_id'];
		if (empty($bot_id)) {
			header("location:index.php?m=cloud&c=cloud_my&a=my_main");
		}

		$wx_group = AllQuery("SELECT * FROM wechat_group WHERE robot_id = {$bot_id}");

		for ($i = 0; $i < count($wx_group); ++$i) {
			$row = $wx_group[$i];
			$group_list .= "<li>";
			$from_name = $row['wechat_room_name'];
			$row_serial = $row['id'];
			$group_list .= "<input class = 'li-input' onclick = send_shop_data({$row_serial}) readonly = 'readonly' type = 'text' value = {$from_name}>";
		}
		
		require($v_file);
	}
}